<?php

/*
 * This class implement the functions like: flirt some people, display who flirted seft or who self flirted. deleten flirt
 * and open the template in the editor.
 */

class FlirtController extends Controller {

    public function init() {
	if (user()->isGuest) {
	    $this->redirect(SITE_NAME_URL);
	}
    }

    /**
     * show all flirt
     * 
     * @param string $type to indicate what kind of view( my flirt or who flirt with me)
     */
    public function actionIndex($type = 'my_flirt') {
	$model = new Flirt('search');
	$model->unsetAttributes();

	if (isset($_GET['Flirt'])) {
	    $model->attributes = $_GET['Flirt'];
	}

	$this->render('index', array('type' => $type, 'model' => $model));
    }

    /**
     * flirt some one
     */
    public function actionSend() {
	if (isset($_POST['receiver']) && $_POST['receiver']) {
	    Flirt::insertFlirt(user()->id, $_POST['receiver']);
	    if (isset($_POST['isPostFacebook']) && $_POST['isPostFacebook']) {
		if (isset($_POST['senderName']) && $_POST['senderName'] && isset($_POST['receiverName']) && $_POST['receiverName']) {
		    //check wheter user connected social
		    $modelSocial = UserOAuth::model()->find('user_id = ' . user()->id . ' and LOWER(provider) = "facebook"');
		    $modelSocial->getAdapter()->setUserStatus('"' . $_POST['senderName'] . '" has flirted "' . $_POST['receiverName'] . '"');
		}
	    }
	}
    }

    /**
     * delete a flirt
     */
    public function actionDelete($id) {
	$model = Flirt::model()->findByPk($id)->delete();
    }

}
